<?php
class cls_control_base
{
	static function get_instance($class)
	{
		global $smarty;
		$cls = "cls_control_{$class}";
		$instance = new $cls();
		$instance->name = $class;
		$instance->smarty = $smarty;
		return $instance;
	}
	
	static function action($action = "")
	{
		$str = explode('.', $action, 2);
		$class = !empty($str[0]) ? $str[0] : 'index';
		$act = isset($str[1]) ? $str[1] : 'show';
		
		switch ("{$class}.{$act}")
		{
			case 'user.login':
			case 'user.show':
			case 'user.logout':
			case "order.show":
			case "order.place_order":
			case "order.get_cart_status":
			case "product.search":
				break;
			default:
				check_login();
				break;
		}
		
		$instance = self::get_instance($class);
		if (method_exists($instance, $act)) {
			$instance->$act();
		} else {
			error_404();
		}
	}
	
	function get_serve($name)
	{
		if (!isset($name)) {
			$name = $this->name;
		}
		return cls_serve_base::get_instance($name);
	}
	
	function serve_action($action, $serve_name)
	{
		if (!isset($serve_name)) {
			$serve_name = $this->name;
		}
		$serve = $this->get_serve($serve_name);
		return $serve->$action();
	}
}